package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/8 20:10
 * https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
 */
public class Title19 {

     class ListNode {
        int val;

        ListNode next;

        public ListNode() {
        }

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode newHead = new ListNode(0);
        newHead.next = head;
        ListNode pre = newHead;
        for (int i = 0; i < n; i++) {
            head = head.next;
        }
        while (head != null) {
            pre = pre.next;
            head = head.next;
        }
        pre.next = pre.next.next;
        return newHead.next;
    }


    public ListNode removeNthFromEnd1(ListNode head, int n) {
        ListNode newHead = new ListNode(-1);
        newHead.next = head;
        ListNode pre = newHead;
        ListNode fast = head;
        for (int i = 0; i < n; i++) {
            fast = fast.next;
        }
        while (fast != null) {
            pre = pre.next;
            fast = fast.next;
        }
        pre.next = pre.next.next;
        return newHead.next;
    }







    public static void main(String[] args) {

    }
}
